﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class VICC_PRULES
    {
        VABKOPF _VABK;
        public VABKOPF VABK
        {
            get
            {
                if (_VABK == null) _VABK = new VABKOPF();
                return _VABK;
            }
        }
        public VZUTEI VZU
        {
            get
            {
                if (!ENVMAIN.ContainsKey("VZU"))
                    ENVMAIN["VZU"] = new VZUTEI();
                return (VZUTEI)ENVMAIN["VZU"];
            }
        }
        VBSGROE VBS31;
        VAPKOPF APK;
        public readonly static VICC_PRULES Instance = new VICC_PRULES(); 
        private DataRow _dwAP = null;
        public DataRow dwAP
        {
            get { return _dwAP; }
            set
            {
                _dwAP = value;
                OnPropertyChanged("dwAP", value);
            }
        }
        private DataRow _dwRule = null;
        private DataRow LastRule;
        public DataRow dwRule
        {
            get { return _dwRule; }
            set
            {
                _dwRule = value;
                OnPropertyChanged("dwRule", value);
            }
        }
        public DataTable TBCSRules = null;
        private string _APZANRKRS; public string APZANRKRS { get { return _APZANRKRS; } set { _APZANRKRS = value; OnPropertyChanged("APZANRKRS", value); } }
        private string _APZALFDNR; public string APZALFDNR { get { return _APZALFDNR; } set { _APZALFDNR = value; OnPropertyChanged("APZALFDNR", value); } }

        public DataSet LOAD(Dictionary<string, object> pm)
        {
            if (((string)pm["ORDERNUM"]).Length < 11)
                return null;
            string order = pm["ORDERNUM"].ToString();
            dwData = TBMain.NewRow();
            dwData["ORDERNUM"] = order;
            SResult = QPRULES02(dwData);
            MergeData(SResult, false);
            return SResult.Copy();
        }

        public bool NEWPLAN(Dictionary<string, object> pm)
        {

            string order = pm["ORDERNUM"].ToString();
            pm["ORDERNUM"] = order;
            RUNID = (string)pm["RUNID"];
            dwData = TBMain.NewRow();
            dwData["ORDERNUM"] = pm["ORDERNUM"].ToString();
            dwData["ARTSTAGE"] = (string)pm["ARTSTAGE"];
            SResult = QPRULES02(dwData);
            MergeData(SResult, true, true);
            //这里数据集合的第一条记录就是dwRule,dwMaster
            if (TBMain.Rows.Count == 0)
            {
                dwData = TBMain.NewRow();
                dwData["ARTSTAGE"] = (string)pm["ARTSTAGE"];
                SResult = QPRULES03(dwData);
                MergeData(SResult, true, true);
                dwRule = TBMain.Rows[0];
                dwMaster = dwRule;
                UPDATEPLAN(pm, true);
                return false;
            }
            else
            {
                dwRule = TBMain.Rows[0];
                dwMaster = dwRule;
                UPDATEPLAN(pm, false);
                return true;
            }
        }

        public void UPDATEPLAN(Dictionary<string, object> pm, bool IsNew = false)
        {

            foreach (DataColumn cm in TBMain.Columns)
            {
                string cname = cm.ColumnName;
                if (cname == "ROWNID") continue;
                if (pm.Keys.Contains(cname))
                {
                    dwRule[cname] = pm[cname].ToString();
                }
            }
            if (IsNew)
            {
                dwRule["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "PR", "  ", 1, 10);
            }
            CONDITIONRULE(pm);
            if (IsNew)
            {
                InsertICC_PRULES(dwRule, app.company);
            }
            else
            {
                UpdateICC_PRULES(dwRule, 0);
            }
            RELOADRULE(dwRule);
        }

        public void CONDITIONRULE(Dictionary<string, object> pm)
        {
            string arts = dwRule["ARTSTAGE"].ToString();
            P4 = pm["VKAUF"].ToString();
            P5 = pm["VKPAUF"].ToString();
            switch (app.company)
            {
                case "YGR":
                    switch (arts)
                    {
                        case "S":
                            CHKYGRVKRULE(pm);
                            break;
                        case "R":
                            CHKRVRULE(pm);
                            break;
                        case "T":
                            CHKTWRULE(pm);
                            break;
                        case "X":
                            CHKXSRULE(pm);
                            break;
                    }
                    break;
                case "JLT":
                    break;
                case "UBT":
                case "SXN":
                    switch (arts)
                    {
                        case "S":
                            QTYEND = VKPQTY;
                            QTYMAT = QTYEND;
                            break;
                        case "R"://坯布
                            JNUM = "1";
                            JWASTGE = "10";
                            break;
                        case "T":
                           
                            break;
                        case "X":
                          
                            break;
                    }
                    break;
            }
        }

        public void RELOADRULE(DataRow _dwRule)
        {
            MessageInfo msg = new MessageInfo();
            msg.MSGTYPE = "RELOADRULE";
            msg.Sender = RUNID;
            msg.ACTION = "RELOADRULE";
            msg.ORDER = _dwRule["ORDERNUM"].ToString();
            msg.Where = app.User;
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

      
        public virtual void CHKTWRULE(Dictionary<string, object> pm)
        {
            P1 = pm["YARNTYP"].ToString();
            P2 = pm["GSFACH"].ToString();
            QTYMAT = QTYEND;//现在的交货数量，是成品需求数量
            //QTYMAT = (Convert.ToDouble(QTYEND) - Convert.ToDouble(F02)).ToString();  
        }
        public virtual void CHKRVRULE(Dictionary<string, object> pm)
        {
            if (TBCSRules == null)
            {
                VICC_AV95RULES.Instance.Load();
                TBCSRules = VICC_AV95RULES.Instance.TBMain.Copy();
            }

            if (pm != null)
            {
                P1 = pm["YARNTYP"].ToString();
                P2 = pm["GSFACH"].ToString();
            }
            else
            {
                if (dwRule == null)
                    return;
            }
            string YARNTYP = P1;//“S"

            double 订单量 = Convert.ToDouble(VKPQTY);//“S" 
            foreach (DataRow row in TBCSRules.Rows)
            {
                //订单量=100
                if (row["YARNTYP"].ToString().Trim() != YARNTYP)
                    continue;
                double WMIN = Convert.ToDouble(row["WMIN"].ToString());//80,130
                double WMAX = Convert.ToDouble(row["WMAX"].ToString());//130,200
                if (WMIN > 订单量)
                    continue;
                if (WMAX < 订单量)
                    continue;
                R01 = row["WASTAGE"].ToString();
            }
            SUBRESETRVR01();
        }
        public void SUBRESETRVR01()
        {
            double 订单量 = Convert.ToDouble(VKPQTY);//“S" 
            QTYMAT = (Helper.RoundX(订单量 * Convert.ToDouble(R01),100)).ToString();
        }
        public virtual void CHKXSRULE(Dictionary<string, object> pm)
        {
            P1 = pm["YARNTYP"].ToString();
            P2 = pm["GSFACH"].ToString();
            QTYMAT = QTYEND;
            P3 = VGASTGA.Instance.LOAD(pm).ToString();
        } 
    }
    public partial class VICC_PRULES
    {
        

        public string ROWNID { get { if (dwMaster == null) return ""; return dwMaster["ROWNID"].ToString(); } set { if (dwMaster == null) return; dwMaster["ROWNID"] = value; OnPropertyChanged("ROWNID", value); } } //lENGth 10
        public string PLANID { get { if (dwMaster == null) return ""; return dwMaster["PLANID"].ToString(); } set { if (dwMaster == null) return; dwMaster["PLANID"] = value; OnPropertyChanged("PLANID", value); } } //lENGth 12
        public string RGROUP { get { if (dwMaster == null) return ""; return dwMaster["RGROUP"].ToString(); } set { if (dwMaster == null) return; dwMaster["RGROUP"] = value; OnPropertyChanged("RGROUP", value); } } //lENGth 4
        public string RULEID { get { if (dwMaster == null) return ""; return dwMaster["RULEID"].ToString(); } set { if (dwMaster == null) return; dwMaster["RULEID"] = value; OnPropertyChanged("RULEID", value); } } //lENGth 3
        public string ARTSTAGE { get { if (dwMaster == null) return ""; return dwMaster["ARTSTAGE"].ToString(); } set { if (dwMaster == null) return; dwMaster["ARTSTAGE"] = value; OnPropertyChanged("ARTSTAGE", value); } } //lENGth 2
        public string ORDERNUM { get { if (dwMaster == null) return ""; return dwMaster["ORDERNUM"].ToString(); } set { if (dwMaster == null) return; dwMaster["ORDERNUM"] = value; OnPropertyChanged("ORDERNUM", value); } } //lENGth 20
        public string ART { get { if (dwMaster == null) return ""; return dwMaster["ART"].ToString(); } set { if (dwMaster == null) return; dwMaster["ART"] = value; OnPropertyChanged("ART", value); } } //lENGth 20
        public string COLOR { get { if (dwMaster == null) return ""; return dwMaster["COLOR"].ToString(); } set { if (dwMaster == null) return; dwMaster["COLOR"] = value; OnPropertyChanged("COLOR", value); } } //lENGth 10
        public string FINISH { get { if (dwMaster == null) return ""; return dwMaster["FINISH"].ToString(); } set { if (dwMaster == null) return; dwMaster["FINISH"] = value; OnPropertyChanged("FINISH", value); } } //lENGth 10
        public string AUFTYP { get { if (dwMaster == null) return ""; return dwMaster["AUFTYP"].ToString(); } set { if (dwMaster == null) return; dwMaster["AUFTYP"] = value; OnPropertyChanged("AUFTYP", value); } } //lENGth 20
        public string QTYEND { get { if (dwMaster == null) return ""; return dwMaster["QTYEND"].ToString(); } set { if (dwMaster == null) return; dwMaster["QTYEND"] = value; OnPropertyChanged("QTYEND", value); } } //lENGth 20
        public string QTYENDDES { get { if (dwMaster == null) return ""; return dwMaster["QTYENDDES"].ToString(); } set { if (dwMaster == null) return; dwMaster["QTYENDDES"] = value; OnPropertyChanged("QTYENDDES", value); } } //lENGth 20
        public string LEADTIME { get { if (dwMaster == null) return ""; return dwMaster["LEADTIME"].ToString(); } set { if (dwMaster == null) return; dwMaster["LEADTIME"] = value; OnPropertyChanged("LEADTIME", value); } } //lENGth 20
        public string DELDATE { get { if (dwMaster == null) return ""; return dwMaster["DELDATE"].ToString(); } set { if (dwMaster == null) return; dwMaster["DELDATE"] = value; OnPropertyChanged("DELDATE", value); } } //lENGth 20
        public string STARTDATE { get { if (dwMaster == null) return ""; return dwMaster["STARTDATE"].ToString(); } set { if (dwMaster == null) return; dwMaster["STARTDATE"] = value; OnPropertyChanged("STARTDATE", value); } } //lENGth 20
        public string UMS { get { if (dwMaster == null) return ""; return dwMaster["UMS"].ToString(); } set { if (dwMaster == null) return; dwMaster["UMS"] = value; OnPropertyChanged("UMS", value); } } //lENGth 20
        public string UNITNUM { get { if (dwMaster == null) return ""; return dwMaster["UNITNUM"].ToString(); } set { if (dwMaster == null) return; dwMaster["UNITNUM"] = value; OnPropertyChanged("UNITNUM", value); } } //lENGth 20
        public string UNDES { get { if (dwMaster == null) return ""; return dwMaster["UNDES"].ToString(); } set { if (dwMaster == null) return; dwMaster["UNDES"] = value; OnPropertyChanged("UNDES", value); } } //lENGth 20
        public string UNITQTY { get { if (dwMaster == null) return ""; return dwMaster["UNITQTY"].ToString(); } set { if (dwMaster == null) return; dwMaster["UNITQTY"] = value; OnPropertyChanged("UNITQTY", value); } } //lENGth 20
        public string UQDES
        {
            get { if (dwMaster == null) return ""; return dwMaster["UQDES"].ToString(); }
            set
            {
                if
                  (dwMaster == null)
                    return;
                dwMaster["UQDES"] = value;
                OnPropertyChanged("UQDES", value);
            }
        } //lENGth 20
        public string TTENDS { get { if (dwMaster == null) return "0"; return dwMaster["TTENDS"].ToString(); } set { if (dwMaster == null) return; dwMaster["TTENDS"] = value; OnPropertyChanged("TTENDS", value); } } //lENGth 20
        public string UENDS { get { if (dwMaster == null) return "0"; return dwMaster["UENDS"].ToString(); } set { if (dwMaster == null) return; dwMaster["UENDS"] = value; OnPropertyChanged("UENDS", value); } } //lENGth 20
        public string R01 { get { if (dwMaster == null) return "0"; return dwMaster["R01"].ToString(); } set { if (dwMaster == null) return; dwMaster["R01"] = value; OnPropertyChanged("R01", value); } } //lENGth 10
        public string R01DES { get { if (dwMaster == null) return ""; return dwMaster["R01DES"].ToString(); } set { if (dwMaster == null) return; dwMaster["R01DES"] = value; OnPropertyChanged("R01DES", value); } } //lENGth 20
        public string R02 { get { if (dwMaster == null) return "0"; return dwMaster["R02"].ToString(); } set { if (dwMaster == null) return; dwMaster["R02"] = value; OnPropertyChanged("R02", value); } } //lENGth 10
        public string R02DES { get { if (dwMaster == null) return "0"; return dwMaster["R02DES"].ToString(); } set { if (dwMaster == null) return; dwMaster["R02DES"] = value; OnPropertyChanged("R02DES", value); } } //lENGth 20
        public string F01 { get { if (dwMaster == null) return "0"; return dwMaster["F01"].ToString(); } set { if (dwMaster == null) return; dwMaster["F01"] = value; OnPropertyChanged("F01", value); } } //lENGth 10
        public string F01DES { get { if (dwMaster == null) return "0"; return dwMaster["F01DES"].ToString(); } set { if (dwMaster == null) return; dwMaster["F01DES"] = value; OnPropertyChanged("F01DES", value); } } //lENGth 20
        public string F02 { get { if (dwMaster == null) return "0"; return dwMaster["F02"].ToString(); } set { if (dwMaster == null) return; dwMaster["F02"] = value; OnPropertyChanged("F02", value); } } //lENGth 10
        public string F02DES { get { if (dwMaster == null) return "0"; return dwMaster["F02DES"].ToString(); } set { if (dwMaster == null) return; dwMaster["F02DES"] = value; OnPropertyChanged("F02DES", value); } } //lENGth 20
        public string JNUM { get { if (dwMaster == null) return "0"; return dwMaster["JNUM"].ToString(); } set { if (dwMaster == null) return; dwMaster["JNUM"] = value; OnPropertyChanged("JNUM", value); } } //lENGth 10
        public string JNUMDES { get { if (dwMaster == null) return "0"; return dwMaster["JNUMDES"].ToString(); } set { if (dwMaster == null) return; dwMaster["JNUMDES"] = value; OnPropertyChanged("JNUMDES", value); } } //lENGth 20
        public string JWASTGE { get { if (dwMaster == null) return "0"; return dwMaster["JWASTGE"].ToString(); } set { if (dwMaster == null) return; dwMaster["JWASTGE"] = value; OnPropertyChanged("JWASTGE", value); } } //lENGth 10
        public string JWASTAGEDES { get { if (dwMaster == null) return "0"; return dwMaster["JWASTAGEDES"].ToString(); } set { if (dwMaster == null) return; dwMaster["JWASTAGEDES"] = value; OnPropertyChanged("JWASTAGEDES", value); } } //lENGth 20
        public string QTYMAT { get { if (dwMaster == null) return "0"; return dwMaster["QTYMAT"].ToString(); } set { if (dwMaster == null) return; dwMaster["QTYMAT"] = value; OnPropertyChanged("QTYMAT", value); } } //lENGth 20
        public string REMARK { get { if (dwMaster == null) return "0"; return dwMaster["REMARK"].ToString(); } set { if (dwMaster == null) return; dwMaster["REMARK"] = value; OnPropertyChanged("REMARK", value); } } //lENGth 20
        public string VKPQTY { get { if (dwMaster == null) return "0"; return dwMaster["VKPQTY"].ToString(); } set { if (dwMaster == null) return; dwMaster["VKPQTY"] = value; OnPropertyChanged("VKPQTY", value); } } //lENGth 20
        public string P0 { get { if (dwMaster == null) return "0"; return dwMaster["P0"].ToString(); } set { if (dwMaster == null) return; dwMaster["P0"] = value; OnPropertyChanged("P0", value); } } //lENGth 100
        public string P1 { get { if (dwMaster == null) return "0"; return dwMaster["P1"].ToString(); } set { if (dwMaster == null) return; dwMaster["P1"] = value; OnPropertyChanged("P1", value); } } //lENGth 20
        public string P2 { get { if (dwMaster == null) return "0"; return dwMaster["P2"].ToString(); } set { if (dwMaster == null) return; dwMaster["P2"] = value; OnPropertyChanged("P2", value); } } //lENGth 20
        public string P3 { get { if (dwMaster == null) return "0"; return dwMaster["P3"].ToString(); } set { if (dwMaster == null) return; dwMaster["P3"] = value; OnPropertyChanged("P3", value); } } //lENGth 20
        public string P4 { get { if (dwMaster == null) return "0"; return dwMaster["P4"].ToString(); } set { if (dwMaster == null) return; dwMaster["P4"] = value; OnPropertyChanged("P4", value); } } //lENGth 20
        public string P5 { get { if (dwMaster == null) return "0"; return dwMaster["P5"].ToString(); } set { if (dwMaster == null) return; dwMaster["P5"] = value; OnPropertyChanged("P5", value); } } //lENGth 20
        public string P6 { get { if (dwMaster == null) return "0"; return dwMaster["P6"].ToString(); } set { if (dwMaster == null) return; dwMaster["P6"] = value; OnPropertyChanged("P6", value); } } //lENGth 20
        public string P7 { get { if (dwMaster == null) return "0"; return dwMaster["P7"].ToString(); } set { if (dwMaster == null) return; dwMaster["P7"] = value; OnPropertyChanged("P7", value); } } //lENGth 20
        public string P8 { get { if (dwMaster == null) return "0"; return dwMaster["P8"].ToString(); } set { if (dwMaster == null) return; dwMaster["P8"] = value; OnPropertyChanged("P8", value); } } //lENGth 20
        public string P9 { get { if (dwMaster == null) return "0"; return dwMaster["P9"].ToString(); } set { if (dwMaster == null) return; dwMaster["P9"] = value; OnPropertyChanged("P9", value); } } //lENGth 20
        public string P10 { get { if (dwMaster == null) return "0"; return dwMaster["P10"].ToString(); } set { if (dwMaster == null) return; dwMaster["P10"] = value; OnPropertyChanged("P10", value); } } //lENGth 100
        public string P11 { get { if (dwMaster == null) return "0"; return dwMaster["P11"].ToString(); } set { if (dwMaster == null) return; dwMaster["P11"] = value; OnPropertyChanged("P11", value); } } //lENGth 20
        public string P12 { get { if (dwMaster == null) return "0"; return dwMaster["P12"].ToString(); } set { if (dwMaster == null) return; dwMaster["P12"] = value; OnPropertyChanged("P12", value); } } //lENGth 20
        public string P13 { get { if (dwMaster == null) return "0"; return dwMaster["P13"].ToString(); } set { if (dwMaster == null) return; dwMaster["P13"] = value; OnPropertyChanged("P13", value); } } //lENGth 20
        public string P14 { get { if (dwMaster == null) return "0"; return dwMaster["P14"].ToString(); } set { if (dwMaster == null) return; dwMaster["P14"] = value; OnPropertyChanged("P14", value); } } //lENGth 20
        public string P15 { get { if (dwMaster == null) return "0"; return dwMaster["P15"].ToString(); } set { if (dwMaster == null) return; dwMaster["P15"] = value; OnPropertyChanged("P15", value); } } //lENGth 20
        public string P16 { get { if (dwMaster == null) return "0"; return dwMaster["P16"].ToString(); } set { if (dwMaster == null) return; dwMaster["P16"] = value; OnPropertyChanged("P16", value); } } //lENGth 20
        public string P17 { get { if (dwMaster == null) return "0"; return dwMaster["P17"].ToString(); } set { if (dwMaster == null) return; dwMaster["P17"] = value; OnPropertyChanged("P17", value); } } //lENGth 20
        public string P18 { get { if (dwMaster == null) return "0"; return dwMaster["P18"].ToString(); } set { if (dwMaster == null) return; dwMaster["P18"] = value; OnPropertyChanged("P18", value); } } //lENGth 20
        public string P19 { get { if (dwMaster == null) return "0"; return dwMaster["P19"].ToString(); } set { if (dwMaster == null) return; dwMaster["P19"] = value; OnPropertyChanged("P19", value); } } //lENGth 20
        public string P20 { get { if (dwMaster == null) return "0"; return dwMaster["P20"].ToString(); } set { if (dwMaster == null) return; dwMaster["P20"] = value; OnPropertyChanged("P20", value); } } //lENGth 100
        public string P21 { get { if (dwMaster == null) return "0"; return dwMaster["P21"].ToString(); } set { if (dwMaster == null) return; dwMaster["P21"] = value; OnPropertyChanged("P21", value); } } //lENGth 20
        public string P22 { get { if (dwMaster == null) return "0"; return dwMaster["P22"].ToString(); } set { if (dwMaster == null) return; dwMaster["P22"] = value; OnPropertyChanged("P22", value); } } //lENGth 20
        public string P23 { get { if (dwMaster == null) return "0"; return dwMaster["P23"].ToString(); } set { if (dwMaster == null) return; dwMaster["P23"] = value; OnPropertyChanged("P23", value); } } //lENGth 20
        public string P24 { get { if (dwMaster == null) return "0"; return dwMaster["P24"].ToString(); } set { if (dwMaster == null) return; dwMaster["P24"] = value; OnPropertyChanged("P24", value); } } //lENGth 20
        public string P25 { get { if (dwMaster == null) return "0"; return dwMaster["P25"].ToString(); } set { if (dwMaster == null) return; dwMaster["P25"] = value; OnPropertyChanged("P25", value); } } //lENGth 20
        public string P26 { get { if (dwMaster == null) return "0"; return dwMaster["P26"].ToString(); } set { if (dwMaster == null) return; dwMaster["P26"] = value; OnPropertyChanged("P26", value); } } //lENGth 20
        public string P27 { get { if (dwMaster == null) return "0"; return dwMaster["P27"].ToString(); } set { if (dwMaster == null) return; dwMaster["P27"] = value; OnPropertyChanged("P27", value); } } //lENGth 20
        public string P28 { get { if (dwMaster == null) return "0"; return dwMaster["P28"].ToString(); } set { if (dwMaster == null) return; dwMaster["P28"] = value; OnPropertyChanged("P28", value); } } //lENGth 20
        public string P29 { get { if (dwMaster == null) return "0"; return dwMaster["P29"].ToString(); } set { if (dwMaster == null) return; dwMaster["P29"] = value; OnPropertyChanged("P29", value); } } //lENGth 20

    }
}
